﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;

using ZedGraph;

namespace StockLearner
{
    static class Utils
    {
        public static DateTime UnixTimeStampToDateTime(double p_unixTimeStamp)
        {
            // Unix timestamp is seconds past epoch
            System.DateTime l_toReturn = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            l_toReturn = l_toReturn.AddSeconds(p_unixTimeStamp).ToLocalTime();
            return l_toReturn;
        }

        public static XDate ConvertDateToXdate(DateTime p_date)
        {
            return new XDate(p_date.ToOADate());
        }

        public static XDate UnixTimestampToXdate(double p_unixTimeStamp)
        {
            return ConvertDateToXdate(UnixTimeStampToDateTime(p_unixTimeStamp));
        }

        public static double DateTimeToUnixTimestamp(DateTime p_date)
        {
            System.DateTime l_epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            return p_date.Subtract(l_epoch).TotalSeconds;
        }

        public static int AddMinutesUnixTimeStamp(int p_unixTimeStamp, double p_toAddMin)
        {
            int l_toReturn = p_unixTimeStamp;
            l_toReturn += (int)(p_toAddMin * 60.0);
            return l_toReturn;
        }

        public static String DblArrayToString(double[] p_toConvert)
        {
            String l_toReturn = "";

            for (int i = 0; i < p_toConvert.Length - 1; i++)
            {
                l_toReturn += p_toConvert[i].ToString("N3") + ",";
            }

            l_toReturn += p_toConvert[p_toConvert.Length - 1].ToString("N3");

            return l_toReturn;
        }

        public static List<YahooDataPoint> DeepCpyYDPList(List<YahooDataPoint> p_toCopy)
        {
            List<YahooDataPoint> l_toReturn = new List<YahooDataPoint>();

            for (int i = 0; i < p_toCopy.Count; i++)
            {
                YahooDataPoint l_toAdd = new YahooDataPoint();

                l_toAdd.Close = p_toCopy[i].Close;
                l_toAdd.Low = p_toCopy[i].Low;
                l_toAdd.High = p_toCopy[i].High;
                l_toAdd.Open = p_toCopy[i].Open;
                l_toAdd.Volume = p_toCopy[i].Volume;
                l_toAdd.Timestamp = p_toCopy[i].Timestamp;

                l_toReturn.Add(l_toAdd);
            }

            return l_toReturn;
        }

        public static double GaussianRandomApprox(double p_mean, double p_stdDev)
        {
            Random l_rand = new Random(); //reuse this if you are generating many
            double l_u1 = l_rand.NextDouble(); //these are uniform(0,1) random doubles
            double l_u2 = l_rand.NextDouble();
            double l_randStdNormal = Math.Sqrt(-2.0 * Math.Log(l_u1)) * Math.Sin(2.0 * Math.PI * l_u2); //random normal(0,1)
            double l_randNormal = p_mean + p_stdDev * l_randStdNormal; //random normal(mean,stdDev^2)

            return l_randNormal;
        }

    }
}
